package com.sitech.openapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.domain.AsyncFeedBack;
import com.sitech.openapi.domain.Test;
import com.sitech.openapi.service.ProdPreOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangsq on 2018/12/5.
 * 关于随选宽带甩单集约销售支撑工作的通知需求
 * 新增订单状态通知 OrderNotification,根据通知类型及状态区分
 * 预受理订单信息发布；
 * 订单竣工通知；
 * 宽带资源确认通知：
 */
@RestController
@Api(tags = "宽带随选预受理订单信息发布.订单状态通知", description = "宽带随选订单状态通知控制层")
@Slf4j
public class ProdPreOrderController {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;
    @Autowired
    private ProdPreOrderService prodPreOrderService;

    @PostMapping("/orderNotification")
    @ApiOperation(value = "甩单API", notes = "预受理订单信息发布")
    public ResponseEntity<Map<String, Object>> orderNotificationBack(@RequestBody String inParamBodys,
                                                                     @RequestHeader HttpHeaders headers) throws Exception{

        log.info("入参<><><><>" + inParamBodys);
        Map inParamBody = new HashMap();
        inParamBody.put("inParamBodys", inParamBodys);
        Map retMap = prodPreOrderService.doOrderNotificationJson(inParamBody);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<Map<String, Object>> res = new ResponseEntity<Map<String, Object>>(retMap, httpHeaders, HttpStatus.OK);
        return res;
    }

    @ExceptionHandler
    public ResponseEntity<Map<String, String>> orderNotificationError(Exception e, NativeWebRequest request) {
        Map<String, String> errors = new HashMap<>();
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message", e.toString());
        errors.put("referenceError", "");

        HttpHeaders httpHeaders = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }
}
